package com.susu.springboot_swagger.controller;

import com.susu.springboot_swagger.entity.Hospital;
import com.susu.springboot_swagger.entity.MedicalAssay;
import com.susu.springboot_swagger.service.HospitalService;
import com.susu.springboot_swagger.service.MedicalAssayService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@Api("sssss")
public class MedicalAssayController {


    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private MedicalAssayService medicalAssayService;



    //查询
    @GetMapping("/getList")
    public String getList(MedicalAssay medicalAssay, Model model){
        List<MedicalAssay> list = medicalAssayService.getList(medicalAssay);
        if (list==null) {
            model.addAttribute("data","查詢失敗");
        } else {
            List<Hospital> hospitalList = hospitalService.getHospitalName();
            model.addAttribute("hospitalName",hospitalList);
            model.addAttribute("data",list);
            model.addAttribute("hospitalId",medicalAssay.getHospitalId());
        }
        return "list";
    }

    //添加
    @PostMapping("/addMedicalAssay")
    public String addMedicalAssay(MedicalAssay medicalAssay,String time){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            medicalAssay.setAssayTime(simpleDateFormat.parse(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        if (medicalAssayService.addMedicalAssay(medicalAssay)) {
            return "redirect:/getList";
        }
        return "/add";
    }

    //  去添加页面
    @GetMapping("/toAdd")
    public String toAdd(Model model){
        List<Hospital> hospitalList = hospitalService.getHospitalName();
        model.addAttribute("hospitalName",hospitalList);
        return "add";
    }

    //确诊
    @GetMapping("/confirmation/{id}")
    @ResponseBody
    public boolean confirmation(@PathVariable Integer id){

        if (medicalAssayService.confirmation(id)) {
            return true;
        }
        return false;
    }




}
